home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / guest-0.000 / guest-0 / guest-0.2 / misc.c < prev    next >
C/C++ Source or Header  |  1995-05-08  |  2KB  |  94 lines

  1. /*
  2. Copyright (C) 1995  Brian Cully
  3.  
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; either version 2 of the License, or (at your option) any later
  7. version.
  8.  
  9. This program is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  12. details.
  13.  
  14. You should have received a copy of the GNU General Public License along with
  15. this program; if not, write to the Free Software Foundation, Inc., 675 Mass
  16. Ave, Cambridge, MA 02139, USA.
  17.  
  18. please send patches or advice to: `shmit@meathook.intac.com'
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <sys/wait.h>
  25. #include <sys/types.h>
  26. #include <string.h>
  27.  
  28. #include "misc.h"
  29. #include "screen.h"
  30.  
  31. /* Print an error message (str) on stderr and exit */
  32. static void error(char *str) {
  33.    fprintf(stderr, "%s", str);
  34.    exit(1);
  35. }
  36.  
  37. /* Finds the named menu
  38. */
  39.  
  40. static int find_menu(struct menu **menu, char *name) {
  41.    struct menu *q=*menu;
  42.  
  43.    while (q->prev)
  44.       q = q->prev;
  45.  
  46.    while (q && (strcmp(q->name, name))) {
  47.       q = q->next;
  48.    }
  49.    
  50.    if (q) {
  51.       *menu = q;
  52.       return 0;
  53.    } else
  54.       return -1;
  55. }
  56.  
  57. /* Interface to screen module
  58.    Execute menu item
  59.    item == item to execute
  60.    *menu == menu list ptr, for use with MENU_SUBs */
  61. void exec_item(struct menu_items item, struct menu **menu) {
  62.    pid_t pid_child;
  63.    int child_stat;
  64.  
  65.    switch (item.type) {
  66.    case MENU_SUB:
  67.       if (!find_menu(menu, item.args)) {
  68.      display_list(*menu);
  69.       }
  70.       else
  71.          fprintf(stderr, "No menu named %s\n", item.name);
  72.       break;
  73.  
  74.    case MENU_EXEC:
  75.       clear_scr();
  76.       close_scr();
  77.       
  78.       if ((pid_child = fork()) == -1)
  79.      error("No more pids\n");
  80.       else if (pid_child > 0) {
  81.      waitpid(pid_child, &child_stat, 0);
  82.       } else {
  83.      system(item.args);
  84.      exit(0);
  85.       }
  86.  
  87.       printf("[----Hit <CR> when ready----]\n");
  88.       getchar();
  89.       init_scr();
  90.       clear_scr();
  91.       break;
  92.    }
  93. }
  94.